package com.chb.service.impl;

import com.chb.dao.InLibrayDao;
import com.chb.dao.StorageVolumeDao;
import com.chb.model.entity.BranchGoods;
import com.chb.model.entity.InLibray;
import com.chb.model.entity.Staff;
import com.chb.model.entity.StorageVolume;
import com.chb.model.vo.PageBean;
import com.chb.model.vo.Result;
import com.chb.service.InLibrayService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.servlet.http.HttpSession;
import java.time.LocalDateTime;
import java.util.Date;
import java.util.List;

/**
 * @author: 陈海彬
 * @date: Created in 2023/9/26 14:59
 * @description:
 * @modified By:
 * @version:
 */
@Service
public class InLibrayServiceImpl implements InLibrayService {
    @Autowired
    private InLibrayDao inLibrayDao;
    @Autowired
    private StorageVolumeDao storageVolumeDao;
    @Autowired
    private HttpSession session;

    /**
     * @param
     * @return com.chb.model.vo.Result
     * @create by: 陈海彬
     * @description: 获取所有入库信息
     * @create time: 2023/9/26 15:04
     */
    @Override
    public Result getAllInLibrayInfo(Integer page, Integer pageSize, Integer branchid) {
        //1.设置分页的大小
        PageHelper.startPage(page, pageSize);
        Staff staff = (Staff) session.getAttribute("staff");
        List<InLibray> inLibrayInfo = null;
        System.out.println("branchid" + branchid);
        //2.进行模糊查询
        if (branchid == null) {
            inLibrayInfo = inLibrayDao.getAllInLibrayInfo(staff);
        } else {
            staff.setBranchid(branchid);
            inLibrayInfo = inLibrayDao.getAllInLibrayInfoByBranchid(staff);
        }
        //3.获取PageInfo
        PageInfo<InLibray> pageInfo = new PageInfo<InLibray>(inLibrayInfo);
        //4.获取返回结果
        PageBean<InLibray> pageBean = new PageBean<InLibray>(pageInfo.getTotal(), pageInfo.getList());
        return Result.success(pageBean);
    }

    /**
     * @param branchGoodsList 门店商品信息列表
     * @create by: 陈海彬
     * @description: 添加入库信息
     * @create time: 2023/9/27 11:06
     */
    @Override
    @Transactional
    public Result addInlibray(List<BranchGoods> branchGoodsList) {
        /*1.获取session*/
        Staff staff = (Staff) session.getAttribute("staff");
        /*2.先向入库信息中添加信息*/
        InLibray inLibray = new InLibray();
        //设置时间
        inLibray.setDate(new Date(System.currentTimeMillis()));
        //设置状态
        inLibray.setStatus(1);
        //设置操作者
        inLibray.setStaffid(staff.getStaffid());
        //设置分店id
        inLibray.setBranchid(staff.getBranchid());
        /*3.执行sql语句*/
        Integer id = inLibrayDao.addInLibray(inLibray);

        for (BranchGoods branchGoods : branchGoodsList) {
            /*4.获得主键id添加另一个表中的数据*/
            StorageVolume storageVolume = new StorageVolume();
            //设置商品id
            storageVolume.setGoodsid(branchGoods.getGoodsid());
            //设置主键id
            storageVolume.setId(inLibray.getId());
            //设置添加的数量
            storageVolume.setNum(branchGoods.getIncounts());
            //设置分店id
            storageVolume.setBranchid(branchGoods.getBranchid());
            /*5.执行sql语句*/
            storageVolumeDao.addStorageVolume(storageVolume);
        }
        return Result.success();
    }

    /**
     * @param id           审核id
     * @param rejectedText 驳回信息
     * @return com.chb.model.vo.Result
     * @create by: 陈海彬
     * @description: 审核驳回
     * @create time: 2023/9/27 19:01
     */
    @Override
    @Transactional
    public Result reviewRejected(Integer id, String rejectedText) {
        InLibray inLibray = new InLibray();
        inLibray.setId(id);
        inLibray.setDates(new Date(System.currentTimeMillis()));
        inLibray.setStatus(3);
        inLibray.setRemark(rejectedText);
        inLibrayDao.updateStatusById(inLibray);
        return Result.success();
    }
}